<template>
  <div>
    <el-dialog
      :title="adddeptsform.id?'编辑部门':'新增部门'"
      :visible="visible"
      width="50%"
      @close="ondialogclose"
    >

      <el-form ref="form" :model="adddeptsform" :rules="adddeptsformrules" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="adddeptsform.name" placeholder="1-10位" />
        </el-form-item>

        <el-form-item label="部门编码" prop="code">
          <el-input v-model="adddeptsform.code" placeholder="1-10位" />
        </el-form-item>

        <el-form-item label="部门负责人" prop="manager">
          <!-- <el-input v-model="adddeptsform.manager" placeholder="请选择" /> -->
          <el-select v-model="adddeptsform.manager" placeholder="请选择" @focus="getdeptsmanager">
            <el-option
              v-for="item in despmanager"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="adddeptsform.introduce" placeholder="1-10位" />
        </el-form-item>
      </el-form>

      <!-- <span>这是一段信息</span> -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('input',false)">取 消</el-button>
        <!-- <el-button type="primary" @click="$emit('input',false)">确 定</el-button> -->
        <el-button type="primary" @click="onsave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getdepartmentslist, adddepartments, getdeptsmentsbyid, editdeptsmentsbyid } from '@/api/departments'
import { getemployeeslistsimple } from '@/api/employees'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    // 校验部门名称
    const valideptname = async(rule, value, callback) => {
      console.log(value)
      let flag = false
      if (this.adddeptsform.id) {
        // 编辑
        const res = await getdepartmentslist()
        const newArr = res.depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id)
        // 根据同部门是否有重名
        flag = newArr.some(item => item.name === value)
      } else {
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }
      if (flag) {
        callback(new Error('不允许部门名称为' + value))
      } else {
        callback()
      }
      callback()
    }

    // const valideptcode = (rule, value, callback) => {
    //   console.log(value)
    //   if (value === '111') return callback(new Error('不允许部门编码为' + value))
    //   callback()
    // }
    // 校验部门编码部门是否存在
    const valideptcode = async(rule, value, callback) => {
      const res = await getdepartmentslist()
      let flag = false
      if (this.adddeptsform.id) {
        const newArr = res.depts.filter(item => item.id !== this.node.id)// 编辑后不能为自己
        console.log(newArr)
        flag = newArr.some(item => item.code === value)
      } else {
        flag = res.depts.some(item => item.code === value)
      }

      // const flag = res.depts.some(item => item.code === value)
      // console.log(value)
      if (flag) return callback(new Error('不允许部门编码为' + value))
      callback()
    }

    return {
      // dialogVisible: false

      node: {
        type: Object,
        required: true
      },
      adddeptsform: {
        code: '',
        data: '',
        manager: '',
        success: ''
      },
      adddeptsformrules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称1-10未小伙子', trigger: 'blur' },
          { validator: valideptname, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码1-10未小伙子', trigger: 'blur' },
          { validator: valideptcode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
          // { message: '部门负责人1-10未小伙子', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门介绍1-10未小伙子', trigger: 'blur' }
        ]

      },
      despmanager: [],
      value: ''
    }
  },

  created() {

  },

  methods: {
    // dialogVisible() {
    //   console.log('确定')
    // }
    ondialogclose() {
      // console.log('消失')
      this.$emit('input', false)
      // 关闭弹窗后重置清空表单数据
      this.$refs.form.resetFields()
      this.adddeptsform = {
        code: '',
        data: '',
        manager: '',
        success: ''
      }
    },
    // 获取焦点点击下拉获取接口数据
    async getdeptsmanager() {
      console.log('获取下拉成功')
      const res = await getemployeeslistsimple()
      this.despmanager = res// 拿到接口把下拉员工赋值
      // console.log(res)
    },
    // 点击确定
    async  onsave() {
      console.log(this.adddeptsform.id)
      if (this.adddeptsform.id) {
        // console.log(123)
        try {
          await this.$refs.form.validate()
          await editdeptsmentsbyid(this.adddeptsform)
          this.ondialogclose()
        } catch (error) {
          console.log(error)
        }
      } else {
        try {
        // 请求的参数pid 使用上面的node.id
          this.adddeptsform.pid = this.node.id
          await adddepartments(this.adddeptsform)
          this.$message.success('添加成功')
          this.$emit('add-success')
        // console.log('成功')
        } catch (error) {
          console.log(error)
        }
      }
    },
    // 查看部门调用最新的数据
    async getdeptsinfo(id) {
      console.log('获取最新的数据', id)
      const res = await getdeptsmentsbyid(id)
      this.adddeptsform = res
      console.log(this.adddeptsform)
    }
  }
}

</script>

<style scoped lang='less'>

</style>
